import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

import createPersistedState from 'vuex-persistedstate'

Vue.use(Vuex)

export default new Vuex.Store({
  plugins: [createPersistedState()],
  state: {
    // token: localStorage.getItem('token')
    token: '',
    // 用户信息
    userinfo: {}
  },
  getters: {
  },
  mutations: {
    updateToken (state, newToken) {
      state.token = newToken

      // localStorage.setItem('token', newToken)
    },
    updateUserInfo (state, newUserInfo) {
      state.userinfo = newUserInfo
    }
  },
  actions: {
    async initUserInfo (context) {
      // ajax获取用户信息
      const { data: res } = await axios.get('/my/userinfo')
      // console.log(res)
      if (res.code === 0) {
        context.commit('updateUserInfo', res.data)
      }
    }
  },
  modules: {
  }
})
